<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="<?php echo $this->_webroot('skin/css/admin.css'); ?>">
<link rel="stylesheet" type="text/css" href="<?php echo $this->_webroot('skin/css/main.css'); ?>">
<input type="hidden" name="attr_add" value="1" >
<input type="hidden" name="attr_append" value="1" >
<style>
td span {color:red;}
.green {color:green;}
.red {color:red;}
td textarea {width:95%; height:42px; font-size:12px;}
td .inputtext{width:120px;}
</style>
<script language="JavaScript" src="<?php echo $this->_webroot('servtools/ajax_client/ajax.js'); ?>"></script>
<script language="JavaScript">
<!--
var cat_id = "<?php echo $cat_id;?>";

function change_cat_id(v) {
	location.href = "<?php echo $this->_url('admin/goods/attributelist/?');?>&cat_id=" + v;
}

function attr_info_cancel(v) {
	var deltr = document.getElementById("tr_"+v);
	deltr.style.display = 'none';
}

function attr_info_save(v) {
	var attr_id = v;
	var attr_name = document.getElementsByName("attr_name["+v+"]")[0];
	var input_type = document.getElementsByName("input_type["+v+"]");
	var attr_values = document.getElementsByName("attr_values["+v+"]")[0];
	var sort_order = document.getElementsByName("sort_order["+v+"]")[0];
	if(attr_name.value=='') { ShowMessageBox("属性名称不能为空!",'0'); return false; }
	var input_type_v = 0;
	for(var i=0; i<input_type.length; i++) {
		if(input_type[i].checked){
			input_type_v = input_type[i].value;
		}
	}
	ajax("<?php echo $this->_url('admin/goods/attributeedit/');?>","POST","attr_id="+attr_id+"&cat_id="+cat_id+"&attr_name="+attr_name.value+"&input_type="+input_type_v+"&attr_values="+attr_values.value+"&sort_order="+sort_order.value,function(data){
		if(data=='-2') {
			ShowMessageBox("对不起，您没有权限不能修改！","m.php?app=error");
			// location.href="m.php?app=error";
		}else{
			if(data=='-1') {
				ShowMessageBox("失败!",'0');
			} else {
				if(attr_id>0) {
					ShowMessageBox("修改成功!",'0');
				} else {
					var tr_0 = document.getElementById("tr_0");
					add_new_attr_info(data,attr_name.value,input_type_v,attr_values.value,sort_order.value)
					attr_name.value = '';
					attr_values.value = '';
					sort_order.value = 0;
					tr_0.style.display = 'none';
					ShowMessageBox("添加成功!",'0');
				}
			}
		}
	});
}

function add_new_attr_info(data,attr_name,input_type,attr_values,sort_order) {
	var attr_tbody = document.getElementById("attr_tbody");
	// 创建新tr
	var newtr = document.createElement("tr");
	newtr.id = "tr_"+data;
	newtr.style.background = "#B4D7E9";

	var td1 = document.createElement("td");
	td1.className = "";
	td1.width="60px";
	td1.innerHTML = data+".";
	newtr.appendChild(td1);

	var td2 = document.createElement("td");
	td2.className = "";
	td2.width="100px";
	td2.innerHTML = '<input type="text" class="small-text" style="width:50px;" name="attr_name['+data+']" value="'+attr_name+'" class="inputtext">';
	newtr.appendChild(td2);

	var td3 = document.createElement("td");
	td3.className = "";
	td3.width="500px";
	if(input_type==0){
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="0" checked="checked" />文本框输入(text)';
	} else {
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="0" />文本框输入(text)';
	}
	if(input_type==1){
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="1" checked="checked" />下拉框选择(select)<br />';
	} else {
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="1" />下拉框选择(select)<br />';
	}
	if(input_type==2){
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="2" checked="checked" />单选框选择(radio)';
	} else {
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="2" />单选框选择(radio)';
	}
	if(input_type==3){
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="3" checked="checked" />多选框选择(checkbox)';
	} else {
		td3.innerHTML += '<input type="radio" name="input_type['+data+']" value="3" />多选框选择(checkbox)';
	}
	newtr.appendChild(td3);

	var td4 = document.createElement("td");
	td4.className = "";
	td4.width="300px";
	td4.innerHTML = '<textarea name="attr_values['+data+']">'+attr_values+'</textarea>';
	newtr.appendChild(td4);

	var td5 = document.createElement("td");
	td5.className = "";
	td5.width="60px";
	td5.align="center";
	td5.innerHTML = '<input type="text" class="small-text" name="sort_order['+data+']" value="'+sort_order+'" style="width:25px;" maxlength="3" />';
	newtr.appendChild(td5);

	var td6 = document.createElement("td");
	td6.className = "";
	td6.width="175px";
	td6.align="center";
	td6.innerHTML = '<input type="button" class="regular-button" value="保存" name="btn['+data+']" onclick="attr_info_save('+data+');" />&nbsp;';
	td6.innerHTML += '<input type="button" class="regular-button" value="删除" name="delbtn['+data+']" onclick="attr_info_del('+data+');" />';
	newtr.appendChild(td6);

	attr_tbody.appendChild(newtr);
}

function attr_info_del(v) {
	if(confirm('确定要删除这个属性吗？')) {
		if(v) {
			ajax("<?php echo $this->_url('admin/goods/attributedel/');?>","POST","id="+v+"&cat_id="+cat_id,function(data){
				if(data=='-2') {
					ShowMessageBox("对不起，您没有权限不能修改！",'m.php?app=error');
					location.href="m.php?app=error";
				}else{
					if(data!='-1') {
						var deltr = document.getElementById("tr_"+v);
						deltr.style.display = 'none';
					} else {
						ShowMessageBox("操作失败!",'0');
					}
				}
			});
		}
	}
}

function attr_info_add() {
	var rights = document.getElementsByName("attr_add")[0].value;
	if(rights != '0'){
		if(cat_id > 0) {
			var tr_0 = document.getElementById("tr_0");
			tr_0.style.display = '';
		} else {
			ShowMessageBox("请先选择产品分类!",'0');
		}
	}else{
		ShowMessageBox("对不起，您没有权限不能修改！",'m.php?app=error');

	}
}

function attr_info_extend(v) {
	var rights = document.getElementsByName("attr_append")[0].value;
	if(rights != '0'){
		if(v>0) {
			if(confirm('您确定要从上级分类属性继承下来吗？它将会把上级分类属性创建到当前分类属性里！')) {
				ajax("<?php echo $this->_url('admin/goods/attributeextend/');?>","POST","parent_id="+v+"&cat_id="+cat_id,function(data){
					if(data=='-1') {
						ShowMessageBox("操作有误,请重试",'0');
					} else if (data=='-2') {
						ShowMessageBox("没有任何可用属性继承下来!",'0');
					} else {
						for(var i=0; i<data.length; i++) {
							add_new_attr_info(data[i].attr_id,data[i].attr_name,data[i].input_type,data[i].attr_values,data[i].sort_order);
						}
					}
				},'JSON');
			}
		} else {
			ShowMessageBox("没有父分类，操作失败！",'0');
		}
	}else{
		ShowMessageBox("对不起，您没有权限不能修改！",'m.php?app=error');
	}
}

//-->
</script>
</head>
<body>
<div id="maincontent">
<psp:tags     htmlname='messagebox'  iconname='messagebox' />
	<div class="wrap">
	<div class="crumbs">当前位置 >> 商品管理 >> 商品属性列表</div>
        <hr />
		<div class="infobox">
    	<h3>商品属性列表</h3>
        <div class="content2">
		<div class="oprate" style="line-height:24px;">&nbsp;&nbsp; 选择商品分类，进行该分类下的商品属性管理。商品属性作为同一种类商品的共有属性供卖家选择。</div>
		<div class="oprate" style="line-height:50px;"><span style="float:right;">

			<input type="button" class="regular-button" value="继承上级分类的属性" onclick="attr_info_extend(<?php echo $parent_id; ?>)" />&nbsp;&nbsp;
			<input type="button" class="regular-button" value="增加新属性" onclick="attr_info_add()" />&nbsp;&nbsp;</span>&nbsp;&nbsp;请先选择产品分类：
			<select name="id" onchange="change_cat_id(this.value);">
				<option value="0">请先选择产品分类</option>
				<?php foreach($category_dg as $value) { ?>
				<option value="<?php echo $value['id']; ?>" <?php if($value['id']==$cat_id) echo "selected"; ?>><?php echo $value['str_pad'].$value['cat_name']; ?></option>
				<?php } ?>
			</select>
		</div>
		<table class="content" id="attr_table">
			<tbody id="attr_tbody">
            <tr>
				<th width="60px">ID</th>
				<th width="100px">属性名称</th>
				<th width="500px">录入方式</th>
				<th width="300px">录入值(多个值时请一行一个)</th>
				<th width="60px" align="center">排序</th>
				<th width="175px" align="center">操作</th>
			</tr>
			<tr id="tr_0" style="display:none; background:#F7C331;">
				<td width="60px">0.</td>
				<td width="100px"><input type="text" class="small-text" style="width:50px;" name="attr_name[0]" value="" class="inputtext"></td>
				<td id="attr_input_type_td" width="500px">
				<?php $i=0;
				foreach($inputtype_arr as $k=>$v) { $i++; ?>
					<input type="radio" name="input_type[0]" value="<?php echo $k; ?>" <?PHP if('0'==$k) {echo "checked";} ?> /><?php echo $v; ?>
				<?php if($i==2) { echo "<br />";}	} ?>
				</td>
				<td width="300px"><textarea name="attr_values[0]"></textarea></td>
				<td width="60px" align="center"><input type="text" class="small-text" name="sort_order[0]" value="0" style="width:25px;" maxlength="3" /></td>
				<td width="175px" align="center">
					<input type="button" class="regular-button" value="保存" name="btn[0]" onclick="attr_info_save(0);" />
					<input type="button" class="regular-button" value="取消" onclick="attr_info_cancel(0);" />
				</td>
			</tr>
			<?php if($attr_info) {
			foreach($attr_info as $value) { ?>
			<tr id="tr_<?php echo $value['attribute']['id'];?>">
				<td width="60px"><?php echo $value['attribute']['id'];?>.</td>
				<td width="100px"><input type="text" class="small-text" style="width:50px;" name="attr_name[<?php echo $value['attribute']['id'];?>]" value="<?php echo $value['attribute']['attr_name'];?>" class="inputtext"></td>
				<td width="500px">
				<?php $i=0;
				foreach($inputtype_arr as $k=>$v) { $i++; ?>
					<input type="radio" name="input_type[<?php echo $value['attribute']['id'];?>]" value="<?php echo $k; ?>" <?PHP if($value['attribute']['input_type']==$k) {echo "checked";} ?> /><?php echo $v; ?>
				<?php if($i==2) { echo "<br />";}	} ?>
				</td>
				<td width="300px"><textarea name="attr_values[<?php echo $value['attribute']['id'];?>]" ><?php echo $value['attribute']['attr_values'];?></textarea></td>
				<td width="60px" align="center"><input type="text" class="small-text" class="small-text" name="sort_order[<?php echo $value['attribute']['id'];?>]" value="<?php echo $value['attribute']['sort_order'];?>" style="width:25px;" maxlength="3" /></td>
				<td width="175px" align="center">
					<input type="button" class="regular-button" value="保存" name="btn[<?php echo $value['attribute']['id'];?>]" onclick="attr_info_save(<?php echo $value['attribute']['id'];?>);" />
					<input type="button" class="regular-button" value="取消" name="delbtn[<?php echo $value['attribute']['id'];?>]" onclick="attr_info_del(<?php echo $value['attribute']['id'];?>)">
				</td>
			</tr>
			<?php }} else { ?>
			<tr>
				<td colspan="6">没有列表!</td>
			</tr>
			<?php } ?>
			</tbody>
		</table>
		<table class="content">
			<tr>
				<td>&nbsp; 注：类型，属性配置完成之后，不要轻易删除修改，会直接影响到用户的商品。</td>
			</tr>
		</table>
		</div>
		</div>
	</div>
</div>
</body>
</html>